package com.isunland.managesystem.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import com.android.volley.toolbox.ImageLoader;
import com.isunland.managesystem.R;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.RequestManager;
import com.isunland.managesystem.entity.ConmpanyEventsPics;
import com.isunland.managesystem.utils.LogUtil;
import java.util.ArrayList;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/5/26 12:18
 */
public class PicsGridViewAdapter extends BaseAdapter {
  private Context context;
  private ArrayList<ConmpanyEventsPics> mList;
  private ImageLoader mImageLoader;

  public PicsGridViewAdapter(Context context, ArrayList<ConmpanyEventsPics> list) {
    this.context = context;
    this.mList = list;
    this.mImageLoader = RequestManager.getImageLoader();
  }

  @Override public int getCount() {
    return mList.size();
  }

  @Override public Object getItem(int i) {
    return mList.get(i);
  }

  @Override public long getItemId(int i) {
    return i;
  }

  @TargetApi(Build.VERSION_CODES.M) @Override
  public View getView(int i, View view, ViewGroup viewGroup) {
    if (view == null) {
      view = LayoutInflater.from(context).inflate(R.layout.gridview_pic, null);
    }
    ConmpanyEventsPics item = mList.get(i);

    ImageView ivPic = (ImageView) view.findViewById(R.id.iv_pic);

    // 图片
    String path = item.getFilepath();

    if (!TextUtils.isEmpty(path)) {
      String urlImage = ApiConst.getDownLoadUrlByGet(path);

      //String filePath = FileUtil.getThumbNailFilePath(path);
      //String urlImage = ApiConst.getDownLoadUrlByGet(filePath);

      LogUtil.i("缩略图===" + urlImage);
      ImageLoader.ImageListener listener = ImageLoader.getImageListener(ivPic, // 控件名
          R.drawable.fail_pic, R.drawable.fail_pic); // 默认图标
      mImageLoader.get(urlImage, listener);
    } else {
      ivPic.setImageResource(R.drawable.fail_pic);
    }
    return view;
  }
}
